﻿using AutoMapper;
using B.S.Filtration.Domain.工单;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ContactCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ContactCommandHander
{
    public class DelContactCommandHander : IRequestHandler<DelContactCommand, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public DelContactCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(DelContactCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var list = await db.Queryable<Contact>().FirstAsync(x => x.ContactId == request.ContactId);
            list.Del = true;

            result.data = await db.Updateable<Contact>(list).ExecuteCommandAsync();

            if (result.data > 0)
            {
                result.code = APIResultCode.success;
                result.msg = "删除成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "删除失败";
            }

            return result;
        }
    }
}
